<?php

namespace app\utils;

use app\utils\Log;

class FTPHelper{

    private static $log;

    public static function upload($local_dir, $ip, $remote_path, $ftp_username, $ftp_password){
        $ftp_conn = ftp_connect($ip);
        if(!$ftp_conn){
            $msg = "无法链接服务器：$ip";
            self::ftpLog($msg);
            return $msg;
        }
        $login = ftp_login($ftp_conn, $ftp_username, $ftp_password);

        if(!$login){
            $msg = "无法登录FTP账号：$ip";
            self::ftpLog($msg);
            return $msg;
        }

        $result = '';

        self::$log = fopen('ftp_log.txt', 'a');
        self::ftpLog("开始上传: $ip");

        if(is_dir($local_dir)){
            self::ftpMkDir($ftp_conn, $remote_path);
            $result .= self::uploadFtpOneFolder($ftp_conn, $local_dir, $remote_path);
        }else{
            $fileName = basename($local_dir);
            $result .= self::uploadFtpOneFile($ftp_conn, $local_dir, $remote_path . '/' . $fileName);
        }
        ftp_close($ftp_conn);
        fclose(self::$log);

        return $result;
    }

    private static function uploadFtpOneFolder($ftp_conn, $local_folder, $remote_path){
        $result = '';
        if($handle = opendir($local_folder)){
            while(false !== ($entry = readdir($handle))){
                if($entry != "." && $entry != ".."){
                    $local_file = $local_folder . '/' . $entry;
                    $remote_file = $remote_path . '/' . $entry;
                    if(is_dir($local_file)){
                        self::ftpLog("$local_file is dir");
                        self::ftpLog("local_file:$local_file remote_file:$remote_file");
                        self::ftpMkDir($ftp_conn, $remote_file);
                        $result .= self::uploadFtpOneFolder($ftp_conn, $local_file, $remote_file . '/');
                    }else{
                        $result .= self::uploadFtpOneFile($ftp_conn, $local_file, $remote_file);
                    }
                }
            }
            closedir($handle);
        }
        return $result;
    }

    private static function uploadFtpOneFile($ftp_conn, $local_file, $remote_file){
        if(!self::startWith($remote_file, '/')){
            $remote_file = '/' . $remote_file;
        }
        self::ftpLog("准备上传文件: $local_file 到 $remote_file");
        if(ftp_put($ftp_conn, $remote_file, $local_file, FTP_BINARY)){
            self::ftpLog("上传文件成功");
            return '';
        }else{
            $error = error_get_last()['message'];
            $msg = "上传文件失败：$local_file $error";
            self::ftpLog($msg);
            return $msg;
        }
    }

    private static function ftpMkDir($ftp_conn, $remote_dir){
        if(!@ftp_chdir($ftp_conn, $remote_dir)){
            ftp_mkdir($ftp_conn, $remote_dir);
            self::ftpLog("创建目录: $remote_dir");
        }
    }

    private static function ftpLog($content){
        Log::info($content);
        fwrite(self::$log, $content . "\n");
    }

    private static function startWith($str, $prefix){
        return substr($str, 0, strlen($prefix)) == $prefix;
    }

}